'use strict'

const _ = require('lodash');
const { parse } = require('csv-parse/sync');
const { TaiSpider } = require('tai-spider');

class SpacetrackSsrSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'spacetrack-ssr';
        this.debug = true;
        this.cookies = []; // for cookies cache
    }

    start_urls() {
        return [{
            link: 'https://www.space-track.org/auth/login',
            skipDuplicates: false,
            direct: true,
        }]
    }

    // set cookies parse
    *parse(response) {
        var cookies = response.cookies;

        this.cookies = cookies;

        let form = JSON.parse(this.options.account).loginForm;

        // try {
        //     const { loginForm = {} } = require('../config/private/spacetrack.json');
        //     form = Object.assign(form, loginForm);
        // } catch (err) {
        //     this.log.error('not found login form, exit with error');
        //     process.exit(1);
        // }

        let headers = {
            "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Google Chrome\";v=\"96\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Linux\"",
            "sec-fetch-dest": "document",
            "sec-fetch-mode": "navigate",
            "sec-fetch-site": "same-origin",
            "sec-fetch-user": "?1",
            "upgrade-insecure-requests": "1",
            'cookie': _.join(_.map(cookies, (item) => `${item.name}=${item.value}`), '; '),
            'Referer': response.uri,
            "Referrer-Policy": "strict-origin-when-cross-origin"
        };

        let item = cookies.filter(item => item.name === 'spacetrack_csrf_cookie');
        if (item.length !== 1) {
            this.log.error('csrf check failed');
            process.exit(1);
        }
        form['spacetrack_csrf_token'] = item[0].value;

        yield response.from_request({
            link: 'https://www.space-track.org/auth/login',
            method: 'POST',
            form,
            headers,
            direct: true,
            cb: this.afterLogin,
        });
    }

    // login result parse
    *afterLogin(response) {
        if (response.statusCode !== 200) {
            this.log.error("Login failed, exit with error.")
            process.exit(1);
        }
        let headers = {
            'cookie': _.join(_.map(this.cookies, (item) => `${item.name}=${item.value}`), '; '),
        };

        yield response.from_request({
            link: 'https://www.space-track.org/basicspacedata/query/class/satcat/predicates/INTLDES,NORAD_CAT_ID,OBJECT_TYPE,SATNAME,COUNTRY,LAUNCH,SITE,DECAY,PERIOD,INCLINATION,APOGEE,PERIGEE,RCSVALUE,COMMENT/CURRENT/Y/orderby/NORAD_CAT_ID%20desc/format/csv/emptyresult/show',
            // download: true,
            options: {
                type: 'csv',
            },
            headers,
            cb: this.parseFile,
        });
    }

    // parse file
    *parseFile(response) {
        let json = parse(response.body, { columns: true })
        for (let item of json) {
            if (item.INTLDES) {
                yield {
                    index_name: 'tai-spacetrack-ssr',
                    id: `${item['INTLDES']}-${item['NORAD_CAT_ID']}`,
                    ...item
                }
            }
        }
        // yield { index_name: 'tai-file', name: 'ssr' }
    }

}

module.exports = SpacetrackSsrSpider;